import { _decorator, Component, Node, Vec2 } from 'cc';
import { ENTITY_STATE_ENUM } from '../../Enum';
import { InputManager } from '../../Base/InputManager';
import { AirState } from './AirState';
const { ccclass, property } = _decorator;

@ccclass('FallState')
export class FallState extends AirState {
    onEnter(): void {
        super.onEnter();
    }
    update(deltaTime: number): void {
        
        if (InputManager.Instance.moveDir.y < 0) {//快速下落
            this.fsm.setGravityScale(this.fsm.player.data.gravityScale * this.fsm.player.data.fastFallGravityMult);
            this.rb.linearVelocity = new Vec2(
                this.rb.linearVelocity.x,
                Math.max(this.rb.linearVelocity.y, -this.fsm.player.data.maxFastFallSpeed)
            );
        }else if (this.rb.linearVelocity.y < 0) { //正常下落
            this.fsm.setGravityScale(this.fsm.player.data.gravityScale * this.fsm.player.data.fallGravityMult);
            this.rb.linearVelocity = new Vec2(
                this.rb.linearVelocity.x,
                Math.max(this.rb.linearVelocity.y, -this.fsm.player.data.maxFallSpeed)
            );
        }

        super.update(deltaTime);

        if (this.fsm.lastOnGroundTime > 0 ) {
            this.fsm.transit(ENTITY_STATE_ENUM.IDLE)
        }
        if (this.fsm.lastOnWallTime > 0 &&
             ((this.fsm.lastOnWallLeftTime > 0 && InputManager.Instance.moveDir.x < 0 ) || 
             (this.fsm.lastOnWallRightTime > 0 && InputManager.Instance.moveDir.x > 0 ))) {
            this.fsm.transit(ENTITY_STATE_ENUM.SLIDE)
        }
        if(this.fsm.lastPressedJumpTime > 0 && this.fsm.lastOnGroundTime > 0){
            this.fsm.transit(ENTITY_STATE_ENUM.JUMP)
        }
    }
    onExit(): void {
        super.onExit(); 
    }
}


